﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;

namespace RBS_Tools
{
    public static class FormToDataRow
    {
        public static DataRow GetRow(DataTable dataTable, NameValueCollection formKeys)
        {
            try
            {
                string[] columnNames = (from DataColumn dataColumn in dataTable.Columns select dataColumn.ColumnName).ToArray();
                string[] queryColumnNames = columnNames.Intersect(formKeys.AllKeys).ToArray();

                Dictionary<string, string> NameValue = new Dictionary<string, string>();
                foreach (var columnName in queryColumnNames)
                {
                    NameValue.Add(columnName, formKeys[columnName]);
                }

                DataRow dataRow = dataTable.NewRow();
                dataRow.BeginEdit();
                foreach (var columns in NameValue.Keys)
                {
                    dataRow[columns] = NameValue[columns];
                }
                dataRow.EndEdit();
                return dataRow;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
